package com.youyun56.gps.android.common;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.util.Log;
import android.view.KeyEvent;

import com.youyun56.gps.android.MainTabActivity;
import com.youyun56.gps.android.RunStep;
import com.youyun56.gps.android.broadcast.CloseSystemDialogBroadCast;
import com.youyun56.gps.android.service.LocationUploadService;

public class BaseActovity extends Activity {
    private final static String     tag = BaseActovity.class.getName();
    protected LocationUploadService service;
    protected SharedPreferences     preference;
    
    protected void dispose() {
        super.onDestroy();
        android.os.Process.killProcess(android.os.Process.myPid());
    }
    
    protected ServiceConnection mServiceConnection = new ServiceConnection() {
                                                       public void onServiceConnected(ComponentName name, IBinder binder) {
                                                           service = ((LocationUploadService.MyBinder) binder).getService();
                                                           service.setRunStep(getRunStep());
                                                           service.setPreference(preference);
                                                           bindEvent();
                                                       }
                                                       
                                                       public void onServiceDisconnected(ComponentName name) {
                                                           
                                                       }
                                                   };
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            new AlertDialog.Builder(this)
            // .setIcon(R.drawable.services)
                .setTitle("提示").setMessage("确定退出程序?").setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                }).setPositiveButton("确定 ", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        LocationUploadService service = MainTabActivity.service;
                        if (service != null && RunStep.NONE != service.getRunStep()) {
                            CloseSystemDialogBroadCast.setOpen(false);
                            service.setRunStep(RunStep.NONE);
                            service.write(CommandFactory.LOGOUT());
                            // if (service.getCar().getPartner() ==
                            // LocationUploadService.gpsUpload) {
                            // unbindService(mServiceConnection);
                            // }
                        }
                        finish();
                    }
                }).show();
            return true;
        }
        return super.onKeyDown(keyCode, event);
        
    }
    
    protected void bindEvent() {
        // TODO Auto-generated method stub
        
    }
    
    protected RunStep getRunStep() {
        // TODO Auto-generated method stub
        return RunStep.MAIN;
    }
    
    protected void startService() {
        Intent intent = new Intent();
        intent.setClass(this, LocationUploadService.class);
        if (!NetWorkHelper.isServiceRunning(this, LocationUploadService.class.getName())) {
            getApplicationContext().startService(intent);
        }
        this.bindService(intent, mServiceConnection, 0);
        Log.i(tag, "bind service");
    }
    
}
